package com.yiyibook.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yiyibook.entity.Book;
import com.yiyibook.entity.BookBigStyle;
import com.yiyibook.entity.BookSmallStyle;
import com.yiyibook.mapper.BookMapper;
import com.yiyibook.mapper.BookSmallStyleMapper;
import com.yiyibook.service.BookService;
import com.yiyibook.util.Constants;
import com.yiyibook.util.ResultEntity;
import com.yiyibook.util.SetPageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 〈功能简述〉
 *
 * @author xun.gong@hand-china.com
 * @date 2019/10/12
 */
@Service
public class BookServiceImpl implements BookService {

    @Autowired
    private BookMapper bookMapper;

    @Autowired
    private BookSmallStyleMapper bookSmallStyleMapper;

    @Override
    public Book selectByBookNum(String bookNum) {
        return bookMapper.selectByBookNum(bookNum);
    }

    @Override
    public int insert(Book book) {
        return bookMapper.insertSelective(book);
    }

    @Override
    public List<Book> selectByBookSmallStyleId(Integer smallStyleId) {
        return bookMapper.selectByBookSmallStyleId(smallStyleId);
    }

    @Override
    public Book selectLastNum() {
        return bookMapper.selectLastNum();
    }

    @Override
    public List<Book> selectByCondition(Book book) {
        return bookMapper.selectByCondition(book);
    }

    @Override
    public List<Book> selectNew() {
        return bookMapper.selectNew();
    }

    @Override
    public Book selectById(Integer bookId) {
        Book book = bookMapper.selectByPrimaryKey(bookId);
        BookSmallStyle bookSmallStyle = bookSmallStyleMapper.selectByPrimaryKey(book.getSmallStyleId());
        book.setBigStyleId(bookSmallStyle.getBigStyleId());
        book.setBigStyleName(bookSmallStyle.getBigStyleName());
        book.setSmallStyleName(bookSmallStyle.getSmallStyleName());
        return book;
    }

    @Override
    public List<Book> selectPop() {
        return bookMapper.selectPop();
    }

    @Override
    public int updateByCondition(Book book) {
        return bookMapper.updateByPrimaryKeySelective(book);
    }

    @Override
    public int deleteById(Integer bookId) {
        return bookMapper.deleteByPrimaryKey(bookId);
    }

    @Override
    public ResultEntity<Book> selectAllByCondition(PageInfo pageInfo, Map<String, String> conditionMap) {
        // 保存查询结果以及分页信息
        ResultEntity<Book> resultEntity = new ResultEntity<>();
        // 当前页
        int pageNum = 1;
        // 页面大小
        int pageSize = 5;
        // 判断是否有分页信息
        Integer[] pageMsg = SetPageInfo.setValue(pageInfo, pageNum, pageSize);
        // 查询条件
        Book book = new Book();
        if(conditionMap!=null){
            Set<String> keySet = conditionMap.keySet();
            for(String key : keySet){
                if(Constants.Condition.BOOK_TITLE.equals(key)){
                    book.setBookTitle(conditionMap.get(key));
                }else if (Constants.Condition.BOOK_NUM.equals(key)){
                    book.setBookNum(conditionMap.get(key));
                }else if (Constants.Condition.INTRODUCTION.equals(key)){
                    book.setIntroduction(conditionMap.get(key));
                }else if (Constants.Condition.AUTHOR.equals(key)){
                    book.setAuthor(conditionMap.get(key));
                }else if (Constants.Condition.BOOK_STOCK.equals(key)){
                    if(conditionMap.get(key) != null && !"".equals(conditionMap.get(key))){
                        book.setStock(Long.parseLong(conditionMap.get(key)));
                    }
                }else if (Constants.Condition.BOOK_PUBLISHING.equals(key)){
                    book.setPublishingCompany(conditionMap.get(key));
                }
                resultEntity.setSearchTitle(key);
                resultEntity.setKeyWord(conditionMap.get(key));
            }
        }
        // 分页插件
        PageHelper.startPage(pageMsg[0],pageMsg[1]);
        // 查询所有的图书大类信息 取 第 pageNum 页 ，页面大小为 pageSize
        List<Book> books = bookMapper.selectAllByCondition(book);
        // 获取新的分页信息
        PageInfo<Book> pageInfoNew = new PageInfo<>(books);
        resultEntity.setEntityList(books);
        resultEntity.setPageInfo(pageInfoNew);
        return resultEntity;
    }

}
